package net.cyue.nest.nestjava.core.injector;

import net.cyue.nest.nestjava.common.annotation.core.Inject;
import net.cyue.nest.nestjava.common.data.InjectableMetaType;
import net.cyue.nest.nestjava.common.data.MetaType;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MetaTypeFactory {

  private static List<MetaType> getMetaTypeDependencies(Class<?> clz) {
    List<MetaType> dependencies = new ArrayList<>();
    Field[] fields = clz.getDeclaredFields();
    for (var f : fields) {
      Inject injectAnnotation = f.getAnnotation(Inject.class);
      if (injectAnnotation == null) {
        continue;
      }
      MetaType mt = new MetaType(
        f.getClass(),
        MetaTypeFactory.getMetaTypeDependencies(f.getClass())
      );
      String name = Injector.getInjectName(injectAnnotation);
      InjectableMetaType iMT = new InjectableMetaType(mt, name);
      dependencies.add(iMT);
    }

    return dependencies;
  }


  public static MetaType getMetaType(Class<?> clz) {
    return new MetaType(
      clz,
      MetaTypeFactory.getMetaTypeDependencies(clz)
    );
  }

  public static InjectableMetaType getInjectableMetaType(Class<?> clz, String name) {
    MetaType mt = MetaTypeFactory.getMetaType(clz);
    return new InjectableMetaType(
      mt,
      name
    );
  }
}

